package com.zh.goods.service.impl;


import com.zh.common.domain.Tree;
import com.zh.common.utils.BuildTree;
import com.zh.goods.dao.GoodsTypeDao;
import com.zh.goods.domain.GoodsTypeDO;
import com.zh.goods.service.GoodsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class GoodsTypeServiceImpl implements GoodsTypeService {
	@Autowired
	private GoodsTypeDao goodsTypeDao;
	
	@Override
	public GoodsTypeDO get(String id){
		return goodsTypeDao.get(id);
	}
	
	@Override
	public List<GoodsTypeDO> list(Map<String, Object> map){
		return goodsTypeDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return goodsTypeDao.count(map);
	}
	
	@Override
	public int save(GoodsTypeDO GoodsType){
		return goodsTypeDao.save(GoodsType);
	}
	
	@Override
	public int update(GoodsTypeDO GoodsType){
		return goodsTypeDao.update(GoodsType);
	}
	
	@Override
	public int remove(String id){
		return goodsTypeDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return goodsTypeDao.batchRemove(ids);
	}

	@Override
	public List<GoodsTypeDO> getAll() {
		return goodsTypeDao.getAll();
	}



	@Override
	public Tree<GoodsTypeDO> getTree() {
		List<Tree<GoodsTypeDO>> trees = new ArrayList<Tree<GoodsTypeDO>>();
		List<GoodsTypeDO> goodsTypes = goodsTypeDao.list(new HashMap<String, Object>(16));
		for (GoodsTypeDO goodsType : goodsTypes) {
			Tree<GoodsTypeDO> tree = new Tree<GoodsTypeDO>();
			tree.setId(goodsType.getId().toString());
			tree.setParentId(goodsType.getPid().toString());
			tree.setText(goodsType.getTypeName());
			Map<String, Object> state = new HashMap<>(16);
			state.put("opened", true);
			state.put("mType", "user");
			tree.setState(state);
			trees.add(tree);
		}

		// 默认顶级菜单为０，根据数据库实际情况调整
		Tree<GoodsTypeDO> t = BuildTree.build(trees);
		return t;
	}

}
